package healththink

class FacilityGroupUserLink{
    FacilityGroup facilityGroup
    User user
    UserType userType

    static constraints = {
        userType(blank:false)
    }

    static mapping = {
        //facilityGroup lazy: false
        //user lazy: false
    }
    
    static FacilityGroupUserLink link(User u, FacilityGroup fg)
    {
        FacilityGroupUserLink fgul = 
            FacilityGroupUserLink.findByUserAndFacilityGroup(
                u,
                fg
            )
        if (!fgul)
        {
            fgul = new FacilityGroupUserLink()
            u.addToFacilityGroups(fgul)
            fg.addToFacilityGroupUsers(fgul)
            fgul.save()
        }
        return fgul
    }
    
    static void unlink(User u, FacilityGroup fg)
    {
        FacilityGroupUserLink fgul = 
            FacilityGroupUserLink.findByUserAndFacilityGroup(
                u,
                fg
            )
            
        if (fgul)
        {
            //u?.removeFromFacilities(fgul)
            //fg?.removeFromFacilitiyGroupUsers(fgul)
            fgul.delete()
        }
    }
}